//
//  RefundViewController.m
//  Youhuiju
//
//  Created by liuovyue on 16/9/8.
//  Copyright © 2016年 yutenys. All rights reserved.
//

#import "RefundViewController.h"
#import <AFHTTPSessionManager.h>
#import "CellUpload.h"
#import "RefundTwoController.h"

@interface RefundViewController ()<UITextFieldDelegate,UICollectionViewDelegate,UICollectionViewDataSource,UIImagePickerControllerDelegate,UINavigationControllerDelegate>


@property (strong, nonatomic) IBOutlet UICollectionView *colllectionUpLoad;
@property (nonatomic, strong) CellUpload *cell;
@property (nonatomic, strong)NSMutableArray *arr;
@property (nonatomic, strong)UILabel *labelTitle;
@property (nonatomic, strong)UIButton *buttonReturn;
@property (nonatomic, strong)UIView *viewBar;
@property (nonatomic, strong)UIButton *buttonSure;
@property (nonatomic, strong)UIView *viewBack;

//退款类型
@property (strong, nonatomic) IBOutlet UILabel *refundTypes;
//我要退款(无需退货)
@property (strong, nonatomic) IBOutlet UIButton *buttonRefund;
@property (strong, nonatomic) IBOutlet UILabel *myRefund;
@property (strong, nonatomic) IBOutlet UIImageView *imageMyRefund;

//我要退货
@property (strong, nonatomic) IBOutlet UILabel *muReturn;
@property (strong, nonatomic) IBOutlet UIButton *buttonReturnGoods;
@property (strong, nonatomic) IBOutlet UIImageView *imageReturnGoods;
//退款原因
@property (strong, nonatomic) IBOutlet UILabel *refundReason;
//请选择退款类型
@property (strong, nonatomic) IBOutlet UILabel *selectRefundTypes;
@property (strong, nonatomic) IBOutlet UIButton *buttonSelectRefundTypes;
//退款金额
@property (strong, nonatomic) IBOutlet UILabel *refundMoney;

//最多49.00
@property (strong, nonatomic) IBOutlet UILabel *refundPrice;

//退款说明
@property (strong, nonatomic) IBOutlet UILabel *refundExplanation;
//(可不填)
@property (strong, nonatomic) IBOutlet UILabel *notFill;
//上传凭证
@property (strong, nonatomic) IBOutlet UIButton *imageUpload;
//提交申请
@property (strong, nonatomic) IBOutlet UIButton *buttonSubmit;

@property (strong, nonatomic)NSMutableAttributedString *attriMyrefund;


@property (nonatomic, strong)UIView *viewBackground;
//请输入退款金额
@property (strong, nonatomic) IBOutlet UITextField *textFieldRefundPrice;
@property (strong, nonatomic) IBOutlet UITextField *textFieldRefundExplation;

//请输入退款说明
@end

@implementation RefundViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    
    [self createNavigationBar];
    self.refundTypes.textColor = [UIColor colorWithHexString:@"333333"];
    self.refundTypes.font = [UIFont systemFontOfSize:14*FitW];
    NSMutableAttributedString *attri = [[NSMutableAttributedString alloc]initWithString:self.refundTypes.text];
    [attri addAttribute:NSForegroundColorAttributeName value:[UIColor colorWithHexString:@"fd487f"] range:NSMakeRange(4,1)];
    
    self.refundTypes.attributedText = attri;
    
    self.myRefund.textColor = [UIColor colorWithHexString:@"333333"];
    self.myRefund.font = [UIFont systemFontOfSize:14*FitW];
    self.attriMyrefund = [[NSMutableAttributedString alloc]initWithString:self.myRefund.text];
    [self.attriMyrefund addAttribute:NSForegroundColorAttributeName value:[UIColor colorWithHexString:@"999999"] range:NSMakeRange(4, 7)];
    self.myRefund.attributedText = self.attriMyrefund;
    self.muReturn.textColor = [UIColor colorWithHexString:@"333333"];
    self.imageMyRefund.alpha = 0;
    self.imageReturnGoods.alpha = 0;
    self.muReturn.font = [UIFont systemFontOfSize:14*FitW];

    
    self.refundReason.textColor = [UIColor colorWithHexString:@"333333"];
    self.refundReason.font = [UIFont systemFontOfSize:14*FitW];
    NSMutableAttributedString *attriReason = [[NSMutableAttributedString alloc]initWithString:self.refundReason.text];
    [attriReason addAttribute:NSForegroundColorAttributeName value:[UIColor colorWithHexString:@"fd487f"] range:NSMakeRange(4, 1)];
    self.refundReason.attributedText = attriReason;
    
    
    self.selectRefundTypes.font = [UIFont systemFontOfSize:14*FitW];
    self.selectRefundTypes.textColor = [UIColor colorWithHexString:@"999999"];
    
    self.refundMoney.textColor = [UIColor colorWithHexString:@"333333"];
    self.refundMoney.font = [UIFont systemFontOfSize:14*FitW];
    NSMutableAttributedString *attriMoney = [[NSMutableAttributedString alloc]initWithString:self.refundMoney.text];
    [attriMoney addAttribute:NSForegroundColorAttributeName value:[UIColor colorWithHexString:@"fd487f"] range:NSMakeRange(4, 1)];
    self.refundMoney.attributedText = attriMoney;
    
    self.refundPrice.textColor = [UIColor colorWithHexString:@"999999"];
    self.refundPrice.font = [UIFont systemFontOfSize:14*FitW];

    self.refundExplanation.textColor = [UIColor colorWithHexString:@"333333"];
    self.refundExplanation.font = [UIFont systemFontOfSize:14*FitW];
    self.notFill.textColor = [UIColor colorWithHexString:@"999999"];
    self.notFill.font = [UIFont systemFontOfSize:12*FitW];

    self.textFieldRefundPrice.delegate = self;
    //self.textFieldRefundExplation.delegate = self;
    //self.textFieldRefundPrice.text = @"123";
    
    [self.colllectionUpLoad registerClass:[CellUpload class] forCellWithReuseIdentifier:@"CellUpload"];
    self.colllectionUpLoad.alpha = 0;
   // self.colllectionUpLoad.backgroundColor = [UIColor yellowColor];
    
    self.colllectionUpLoad.delegate = self;
    self.colllectionUpLoad.dataSource = self;
    
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField

{
    
       [self.textFieldRefundExplation resignFirstResponder];
    return YES;
    
}

- (IBAction)resignKeyBoredAction:(id)sender {
    
    
    [[UIApplication sharedApplication] sendAction:@selector(resignFirstResponder) to:nil from:nil forEvent:nil];
}



//输入最多金额条件限定

- (void)textFieldDidEndEditing:(UITextField *)textField {
//    NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"([^0-9\u4e00-\u9fff])" options:0 error:nil];
//    NSString *resString = [regex stringByReplacingMatchesInString:self.textFieldRefundPrice.text options:0 range:NSMakeRange(0, self.textFieldRefundPrice.text.length) withTemplate:@""];
//    if (![self.textFieldRefundPrice.text isEqualToString:resString]) {
//        NSLog(@"dddff");
//    }
    
    int i = [self.textFieldRefundPrice.text intValue];
    if (i > 49) {
        self.textFieldRefundPrice.text = nil;
    }
}

- (BOOL)textFieldShouldEndEditing:(UITextField *)textField{
    
    
    
    //返回BOOL值，指定是否允许文本字段结束编辑，当编辑结束，文本字段会让出first responder
    
    //要想在用户结束编辑时阻止文本字段消失，可以返回NO
    
    //这对一些文本字段必须始终保持活跃状态的程序很有用，比如即时消息
    
    
    NSLog(@"bbbbb");
    return YES;
    
}


#pragma mark - 退款类型显示
- (IBAction)buttonRefundAction:(id)sender {
    
    [self.attriMyrefund addAttribute:NSForegroundColorAttributeName value:[UIColor colorWithHexString:@"fd487f"] range:NSMakeRange(0, 4)];
    self.myRefund.attributedText = self.attriMyrefund;

    self.imageMyRefund.alpha = 1;
    self.imageReturnGoods.alpha = 0;
    
    self.muReturn.textColor = [UIColor colorWithHexString:@"333333"];
    
    
}


- (IBAction)buttonReturnGoods:(id)sender {
    [self.attriMyrefund addAttribute:NSForegroundColorAttributeName value:[UIColor colorWithHexString:@"333333"] range:NSMakeRange(0, 4)];
    self.myRefund.attributedText = self.attriMyrefund;

    self.imageMyRefund.alpha = 0;
    self.imageReturnGoods.alpha = 1;
    
    self.muReturn.textColor = [UIColor colorWithHexString:@"fd487f"];
    
}


- (IBAction)buttonRefundTypes:(id)sender {
    NSLog(@"dddd");
    
    self.viewBackground = [[UIView alloc]initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height)];
    
    self.viewBackground.backgroundColor =  [UIColor colorWithRed:0.7037 green:0.7037 blue:0.7037 alpha:0.375565732758621];
    [self.view addSubview:self.viewBackground];
    UIButton *buttonBack = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.viewBackground addSubview:buttonBack];
    [buttonBack mas_makeConstraints:^(MASConstraintMaker *make) {
       
        make.edges.mas_equalTo(self.viewBackground).insets(UIEdgeInsetsMake(0, 0, 0, 0));
        
        
        
    }];
    
    
    [buttonBack addTarget:self action:@selector(removeBack:) forControlEvents:UIControlEventTouchUpInside];
    
    
    
    UIView *viewFormat = [[UIView alloc]init];
    viewFormat.backgroundColor = [UIColor whiteColor];
    
    [viewFormat.layer setCornerRadius:6.0];
    [buttonBack addSubview:viewFormat];
    [viewFormat mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.equalTo(self.viewBackground.mas_top).offset(250*FitH);
        make.bottom.equalTo(self.viewBackground.mas_bottom).offset(-275*FitH);
        make.right.equalTo(self.viewBackground.mas_right).offset(-60*FitW);
        make.left.equalTo(self.viewBackground.mas_left).offset(60*FitW);
    }];

    
    
    UILabel *labelRefund = [[UILabel alloc]init];
    labelRefund.text = @"退款原因";
    labelRefund.textColor = [UIColor colorWithHexString:@"333333"];
    labelRefund.font = [UIFont systemFontOfSize:21*FitW];
    
    [viewFormat addSubview:labelRefund];
    labelRefund.textAlignment = NSTextAlignmentCenter;
    [labelRefund mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(viewFormat.mas_top).offset(35*FitH);
        make.left.equalTo(viewFormat.mas_left);
        make.right.equalTo(viewFormat.mas_right);
        
        
    }];
    
    
    UIButton *buttonMoreTake = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.viewBackground addSubview:buttonMoreTake];
    [buttonMoreTake setImage:[UIImage imageNamed:@"Taken-more"] forState:UIControlStateNormal];
    [viewFormat addSubview:buttonMoreTake];
    [buttonMoreTake mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.equalTo(labelRefund.mas_bottom).offset(28*FitH);
        make.centerX.equalTo(viewFormat.mas_centerX);
        make.width.mas_equalTo(181*FitW);
        make.height.mas_equalTo(36*FitH);
        
    }];
    
    
    [buttonMoreTake addTarget:self action:@selector(moreTakeBack:) forControlEvents:UIControlEventTouchUpInside];
    
    
    
    
    
}

- (void)removeBack:(UIButton *)button {
    [self.viewBackground removeFromSuperview];
}
- (void)moreTakeBack:(UIButton *)button {
    
   
    self.selectRefundTypes.text = @"多拍/错牌/不想要";
    [self.viewBackground removeFromSuperview];
}

#pragma mark -  上传凭证
- (IBAction)upLoadAction:(id)sender {
    NSLog(@"上传凭证");
    [self changeUpload ];
}

#pragma mark - 提交申请
- (IBAction)puUpAction:(id)sender {
  
 
    RefundTwoController *refundDone = [[RefundTwoController alloc]init];
    [self.navigationController pushViewController:refundDone animated:YES];
    
}



- (void)createNavigationBar{
    
    self.viewBar = [[UIView alloc]initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, 64)];
    self.viewBar.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:self.viewBar];
    
    
    
    
    
    
    self.labelTitle = [[UILabel alloc]init];
    self.labelTitle.text = @"申请退款";
    self.labelTitle.textAlignment = NSTextAlignmentCenter;
    self.labelTitle.textColor = [UIColor blackColor];
    self.labelTitle.font = [UIFont systemFontOfSize:18*FitW];
    [self.viewBar addSubview:self.labelTitle];
    
    [_labelTitle mas_makeConstraints:^(MASConstraintMaker *make) {
        
        //make.top.equalTo(self.viewBar.mas_top);
        make.left.equalTo(self.viewBar.mas_left).offset(100*FitW);
        make.right.equalTo(self.viewBar.mas_right).offset(-100*FitW);
        make.bottom.equalTo(self.viewBar.mas_bottom).offset(-14*FitH);
        
        
        
    }];
    //返回按钮
    self.buttonReturn = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.buttonReturn setImage:[UIImage imageNamed:@"zfanhuijian"] forState:UIControlStateNormal];
    [self.buttonReturn addTarget:self action:@selector(tuihuiAcion:) forControlEvents:UIControlEventTouchUpInside];
    [self.viewBar addSubview:self.buttonReturn];
    
    [_buttonReturn mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.equalTo(self.viewBar.mas_left).offset(17*FitW);
        make.width.mas_equalTo(22*FitW);
        make.height.mas_equalTo(22*FitH);
        make.bottom.equalTo(self.viewBar.mas_bottom).offset(-13*FitH);
        
        
        
    }];
    
  
    
    
}

- (void)tuihuiAcion:(UIButton *)button {
    [self.navigationController popViewControllerAnimated:YES];
}


#pragma mark - 上传图片
- (void)createUpLoad {
AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json",@"text/html",@"image/jpeg",@"image/png",@"application/octet-stream",@"text/json", nil];

    NSString *url = @"http//";
    NSDictionary *dic = [NSDictionary dictionary];
    
    NSURLSessionDataTask *atsk = [manager POST:url parameters:dic progress:^(NSProgress * _Nonnull uploadProgress) {
        
        
        
        
        
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        
        
        
        
        
        
        
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
        
        
        
        
    }];
    
    
    
    
    
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return 3;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    self.cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"CellUpload" forIndexPath:indexPath];
   
    NSLog(@"%@",self.cell.imagePic.image);

    return self.cell;
}

-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    return CGSizeMake(45 * FitW, 45 * FitH);
    
}

- (void)changeUpload {
    __block NSUInteger blockSourceType = 0;
    blockSourceType = UIImagePickerControllerSourceTypePhotoLibrary;
    UIImagePickerController *imagePickerController = [[UIImagePickerController alloc]init];
    imagePickerController.delegate = self;
    imagePickerController.allowsEditing = YES;
    
    
    imagePickerController.sourceType = blockSourceType;
    [self presentViewController:imagePickerController animated:YES completion:nil];
    
    
    
}



- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info{
    [picker dismissViewControllerAnimated:YES completion:nil];
    
    UIImage *image = [info objectForKey:UIImagePickerControllerEditedImage];
    self.arr = [NSMutableArray array];
    self.cell.imagePic.image = image;
    [self.arr addObject:self.cell.imagePic];
    NSLog(@"bbbb  %ld",self.arr.count);
    //NSLog(@"%@",self.cell.imagePic.image);
    
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
